define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var assistantId = 0;
    var Controller = {
        index: function () {
            assistantId = $('#assistantId').val();
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'assistant/order/index' + location.search + '&sid='+assistantId,
                    // add_url: 'assistant/order/add',
                    // edit_url: 'assistant/order/edit',
                    // del_url: 'assistant/order/del',
                    // multi_url: 'assistant/order/multi',
                    // import_url: 'assistant/order/import',
                    table: 'assistant_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'assistant.nickname', title: __('Assistant.nickname'), operate: 'LIKE'},
                        {field: 'user.username', title: __('User.username'), operate: 'LIKE'},
                        {field: 'order', title: __('Order'), operate: false},
                        {field: 'orderer', title: __('Orderer')},
                        {field: 'order_id', title: __('Order_id')},
                        {field: 'amount', title: __('Amount'), operate:'BETWEEN'},
                        {field: 'discount_amount', title: '优惠金额', operate:'BETWEEN'},
                        {field: 'real_amount', title: '真实金额', operate:'BETWEEN'},
                        {field: 'distance_amount', title: '路费', operate:'BETWEEN'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4')}, formatter: Table.api.formatter.status},
                        {field: 'pay_status', title: __('Pay_status'), searchList: {"0":__('Pay_status 0'),"1":__('Pay_status 1'),"2":__('Pay_status 2')}, formatter: Table.api.formatter.status},
                        {field: 'order_start_time', title: __('Order_start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'order_end_time', title: __('Order_end_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'server_start_time', title: __('Server_start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'server_end_time', title: __('Server_end_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'delete', title: __('Delete'), searchList: {"0":__('Delete 0'),"1":__('Delete 1')}, formatter: Table.api.formatter.normal},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,buttons:[
                                {
                                    name:'开始',
                                    title:'开始',
                                    text:'开始',
                                    icon:'fa fa-magic',
                                    classname:'btn btn-xs btn-info btn-ajax',
                                    visible:function(row){
                                        if(row.status == 1 && row.pay_status == 1){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    url:'assistant/order/start',
                                    refresh:true
                                },
                                {
                                    name:'结束',
                                    title:'结束',
                                    icon:'fa fa-magic',
                                    text:'结束',
                                    classname:'btn btn-xs btn-info btn-ajax',
                                    visible:function(row){
                                        if(row.status == 3 && row.pay_status == 1 ){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    url:'assistant/order/end',
                                    refresh:true
                                },
                                {
                                    name:'退单',
                                    title:'退单',
                                    icon:'fa fa-magic',
                                    text:'退单',
                                    classname:'btn btn-xs btn-info btn-ajax',
                                    visible:function(row){
                                        if(row.status < 2 && row.pay_status == 1){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    url:'assistant/order/refund',
                                    refresh:true
                                },

                                {
                                    name:'接单',
                                    extend:'data-area=["90%","90%"]',
                                    title:'接单',
                                    icon:'fa fa-magic',
                                    text:'接单',
                                    classname:'btn btn-xs btn-success btn-ajax',
                                    visible:function(row){
                                        if(row.status == 0 && row.pay_status == 1){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    url:'assistant/order/accept',
                                    refresh:true
                                }
                            ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
